<template>
	<view class="content">
		<view class="invokeRow cross-center">
			<text class="invokeLable">银行名称</text>
			<input class="invokeInput" :value="card.bankName" @input="inpugChange" data-key='bankName'></input>
		</view>
		<view class="invokeRow cross-center">
			<text class="invokeLable">银行卡号</text>
			<input class="invokeInput" :value="card.accNo" @input="inpugChange" data-key='accNo'></input>
		</view>
		<view class="invokeRow cross-center">
			<text class="invokeLable">开户人</text>
			<input class="invokeInput" :value="card.accUser" @input="inpugChange" data-key='accUser'></input>
		</view>
		<view class="invokeRow cross-center">
			<text class="invokeLable">开户支行</text>
			<input class="invokeInput" :value="card.bankArea" @input="inpugChange" data-key='bankArea'></input>
		</view>
		<view class="modifModle" v-if="oprate=='修改'">
			<view class="del" @click="deleteCard">
				删除
			</view>
			<view class="modify" @click="confirm">
				{{oprate}}
			</view>
		</view>
		<button v-else class="bottombtn" @click="confirm">确认{{oprate}}</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				card: {},
				oprate: '添加',
				allow: true
			};
		},
		onLoad(option) {
			if (option.card) {
				this.card = JSON.parse(option.card)
				this.oprate = "修改"
				uni.setNavigationBarTitle({
					title: '修改银行卡信息'
				})
			}
		},
		methods: {
			inpugChange(e) {
				let key = e.currentTarget.dataset.key;
				this.card[key] = e.detail.value;
			},
			deleteCard() {
				let that = this
				uni.showModal({
					content: '确认要删除该银行卡吗？',
					cancelText: '算了',
					confirmText: '删除',
					success: (e) => {
						if (e.confirm) {
							that.$Request.request('/api/user_center/bankCardDel', "POST", {
								id: that.card.id
							}).then(res => {
								if (res.status == 1) {

									console.log(res)
									this.$api.msg("删除成功！")
									setTimeout(() => {
										uni.navigateBack()
									}, 800)
								}
							}).catch(err => {
								console.log(err)
							})
						}
					}
				})
			},
			confirm() {
				let cardReg = /^([1-9]{1})(\d{14}|\d{18})$/
				if (!this.allow) {
					return
				}
				if (!this.card.bankName) {
					this.$api.msg('请输入银行名称！')
					return
				}
				if (!cardReg.test(this.card.accNo)) {
					this.$api.msg('请输入正确的银行卡号！')
					return
				}
				if (!this.card.accUser) {
					this.$api.msg('请输入开户人名称！')
					return
				}
				if (!this.card.bankArea) {
					this.$api.msg('请输入开户支行名称！')
					return
				}
				this.allow = false
				let that = this
				that.$Request.request('/api/user_center/bankCardSave', 'POST', that.card).then(res => {
					console.log(res)
					if (res.status == 1) {
						that.$api.msg(`银行卡${that.oprate}成功!`)
						setTimeout(() => {
							uni.navigateBack()
							that.allow = true
						}, 800)
					}

				}).catch(err => {
					console.log(err)
					that.allow = true
				})
			}
		}
	}
</script>

<style lang="scss">
	page,
	.content {
		height: 100%;
	}

	.invokeRow {
		background-color: #ffffff;
		height: 99rpx;
		padding: 0 24rpx;
		box-sizing: border-box;
		font-size: 28rpx;
		border-bottom: 1rpx solid #e2e2e2;
		color: #353535;
		@extend %flexVcenter;
	}

	.modifModle {
		display: flex;
		color: #fff;
		font-size: $uni-font-size-lg;
		width: 710rpx;
		margin: 40rpx auto;
		border-radius: 8rpx;
		overflow: hidden;

		view {
			width: 50%;
			letter-spacing: 0.2em;
			@include textCenter(90rpx);
		}

		.del {
			background-color: $uni-color-primary;
		}

		.modify {
			background-color: $themColor;
		}
	}

	.invokeLable {
		width: 124rpx;
	}

	.invokeInput {
		width: 580rpx;
		@extend %flexVcenter;
		align-items: center;
		padding-left: 30rpx;
	}

	.invokePicker {
		width: 520rpx;
		height: 99rpx;
	}

	.inner {
		line-height: 99rpx;
		text-align: right;
		padding-right: 1.5em;
	}

	.iconCon {
		width: 30rpx;
		display: flex;
		justify-content: center;
	}

	.icon {
		width: 12rpx;
		height: 24rpx;
	}

	.bottombtn {
		margin: 50rpx auto 0 auto;
		background-color: $themColor;
		width: 660rpx;
		border-radius: 100rpx;
		color: #fff;
		letter-spacing: 1em;
	}
</style>
